using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace ICP_MS.Plugin.AnalyteDb;

public class MassDisallowedControl : UserControl
{
	private List<PointF> disallowedRange = new List<PointF>();

	private int selectIndex = -1;

	private float border = 10f;

	private Point currentMouse = Point.Empty;

	private IContainer components = null;

	private Button btnLast;

	private Button btnNext;

	private NumericUpDown nudLeft;

	private NumericUpDown nudRight;

	private PictureBox pictureBox1;

	public List<PointF> DisallowedRange => disallowedRange;

	public event EventHandler disallowedRangeChanged = null;

	public MassDisallowedControl()
	{
		InitializeComponent();
	}

	private void pictureBox1_Paint(object sender, PaintEventArgs e)
	{
		float width = pictureBox1.Width;
		float height = pictureBox1.Height;
		Pen pen = new Pen(Color.DarkGreen, 2f);
		e.Graphics.DrawLine(pen, border, height / 2f, width - border, height / 2f);
		float w = (width - border * 2f) / 26f;
		for (int j = 0; j <= 26; j++)
		{
			float graduate = ((j % 5 == 0) ? 8 : 5);
			e.Graphics.DrawLine(pen, border + (float)j * w, height / 2f, border + (float)j * w, height / 2f - graduate);
			if (j % 5 == 0)
			{
				e.Graphics.DrawString((j * 10).ToString(), new Font("SimSun", 10.5f), new SolidBrush(Color.Black), border + (float)j * w - 10f, height / 2f - 20f);
			}
		}
		if ((float)currentMouse.X >= border && (float)currentMouse.X <= width - border)
		{
			e.Graphics.DrawString((((float)currentMouse.X - border) / (width - border * 2f) * 260f).ToString("F2"), new Font("SimSun", 8.5f), new SolidBrush(Color.DarkGreen), currentMouse.X, height / 2f - 20f);
		}
		for (int i = 0; i < disallowedRange.Count; i++)
		{
			float k = disallowedRange[i].X;
			float r = disallowedRange[i].Y;
			float x1 = k * (width - border * 2f) / 260f + border;
			float x2 = r * (width - border * 2f) / 260f + border;
			e.Graphics.FillRectangle(new SolidBrush(Color.Red), x1, height / 2f - 5f, x2 - x1, 10f);
			if (i == selectIndex)
			{
				e.Graphics.DrawRectangle(new Pen(Color.Blue), x1, height / 2f - 5f, x2 - x1, 10f);
			}
		}
	}

	private void SetEditValue()
	{
		if (selectIndex >= 0 && selectIndex < disallowedRange.Count)
		{
			try
			{
				nudLeft.ValueChanged -= Range_ValueChanged;
				nudRight.ValueChanged -= Range_ValueChanged;
				nudLeft.Value = Convert.ToDecimal(disallowedRange[selectIndex].X);
				nudRight.Value = Convert.ToDecimal(disallowedRange[selectIndex].Y);
				nudLeft.ValueChanged += Range_ValueChanged;
				nudRight.ValueChanged += Range_ValueChanged;
			}
			catch
			{
			}
		}
	}

	private void btnLast_Click(object sender, EventArgs e)
	{
		if (selectIndex > 0)
		{
			selectIndex--;
		}
		SetEditValue();
		pictureBox1.Refresh();
	}

	private void btnNext_Click(object sender, EventArgs e)
	{
		if (selectIndex < disallowedRange.Count - 1)
		{
			selectIndex++;
		}
		SetEditValue();
		pictureBox1.Refresh();
	}

	private void Range_ValueChanged(object sender, EventArgs e)
	{
		float i = decimal.ToSingle(nudLeft.Value);
		float r = decimal.ToSingle(nudRight.Value);
		if (i > r)
		{
			float t = i;
			i = r;
			r = t;
		}
		if (selectIndex >= 0 && selectIndex < disallowedRange.Count)
		{
			disallowedRange[selectIndex] = new PointF(i, r);
			AdjustRange();
			SelectRange((i + r) / 2f);
		}
		pictureBox1.Refresh();
		ChangedNotice();
	}

	private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
	{
		float x = ((float)e.X - border) / ((float)pictureBox1.Width - border * 2f) * 260f;
		if (e.Button == MouseButtons.Left)
		{
			SelectRange(x);
		}
		else if (e.Button == MouseButtons.Right)
		{
			ContextMenu menu = new ContextMenu();
			menu.MenuItems.Add(Lang.Txt("FormRatio.Button.Add.Text"), delegate
			{
				AddRange(x);
				pictureBox1.Refresh();
			});
			menu.MenuItems.Add(Lang.Txt("FormRatio.Delete.Text"), delegate
			{
				RemoveRange(x);
				pictureBox1.Refresh();
			});
			menu.Show(pictureBox1, e.Location);
		}
	}

	private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
	{
		currentMouse = e.Location;
		pictureBox1.Refresh();
	}

	private void SelectRange(float x)
	{
		int index = -1;
		for (int i = 0; i < disallowedRange.Count; i++)
		{
			PointF pt = disallowedRange[i];
			if (pt.X <= x && pt.Y >= x)
			{
				index = i;
				break;
			}
		}
		if (index != -1)
		{
			selectIndex = index;
			SetEditValue();
		}
	}

	private void AddRange(float x)
	{
		float i = (float)(int)(x + 0.5f) - 0.5f;
		float r = (float)(int)(x + 0.5f) + 0.5f;
		disallowedRange.Add(new PointF(i, r));
		AdjustRange();
		SelectRange(x);
		ChangedNotice();
	}

	private void DeleteSelectRange()
	{
		if (selectIndex >= 0 && selectIndex < disallowedRange.Count)
		{
			disallowedRange.RemoveAt(selectIndex);
			selectIndex--;
		}
	}

	private void RemoveRange(float x)
	{
		int index = -1;
		for (int i = 0; i < disallowedRange.Count; i++)
		{
			PointF pt = disallowedRange[i];
			if (pt.X <= x && pt.Y >= x)
			{
				index = i;
				break;
			}
		}
		if (index != -1)
		{
			disallowedRange.RemoveAt(index);
			if (selectIndex >= index)
			{
				selectIndex--;
			}
		}
		else
		{
			DeleteSelectRange();
		}
		ChangedNotice();
	}

	private void AdjustRange()
	{
		if (disallowedRange.Count <= 1)
		{
			return;
		}
		SortRange();
		for (int i = 0; i < disallowedRange.Count - 1; i++)
		{
			PointF pt0 = disallowedRange[i];
			PointF pt1 = disallowedRange[i + 1];
			if (pt0.Y >= pt1.X)
			{
				pt0.Y = ((pt0.Y >= pt1.Y) ? pt0.Y : pt1.Y);
				disallowedRange[i] = pt0;
				disallowedRange.RemoveAt(i + 1);
				i--;
			}
		}
	}

	private void SortRange()
	{
		disallowedRange.Sort(delegate(PointF pt0, PointF pt1)
		{
			if (pt0.X < pt1.X)
			{
				return -1;
			}
			return (pt0.X != pt1.X) ? 1 : 0;
		});
	}

	private void ChangedNotice()
	{
		if (this.disallowedRangeChanged != null)
		{
			this.disallowedRangeChanged(this, null);
		}
	}

	public void SelectFirst()
	{
		if (disallowedRange.Count > 0)
		{
			selectIndex = 0;
			SetEditValue();
			Invoke((EventHandler)delegate
			{
				Refresh();
			});
		}
	}

	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	private void InitializeComponent()
	{
		this.btnLast = new System.Windows.Forms.Button();
		this.btnNext = new System.Windows.Forms.Button();
		this.nudLeft = new System.Windows.Forms.NumericUpDown();
		this.nudRight = new System.Windows.Forms.NumericUpDown();
		this.pictureBox1 = new System.Windows.Forms.PictureBox();
		((System.ComponentModel.ISupportInitialize)this.nudLeft).BeginInit();
		((System.ComponentModel.ISupportInitialize)this.nudRight).BeginInit();
		((System.ComponentModel.ISupportInitialize)this.pictureBox1).BeginInit();
		base.SuspendLayout();
		this.btnLast.Location = new System.Drawing.Point(3, 3);
		this.btnLast.Name = "btnLast";
		this.btnLast.Size = new System.Drawing.Size(25, 41);
		this.btnLast.TabIndex = 0;
		this.btnLast.Text = "<<";
		this.btnLast.UseVisualStyleBackColor = true;
		this.btnLast.Click += new System.EventHandler(btnLast_Click);
		this.btnNext.Location = new System.Drawing.Point(27, 3);
		this.btnNext.Name = "btnNext";
		this.btnNext.Size = new System.Drawing.Size(25, 41);
		this.btnNext.TabIndex = 1;
		this.btnNext.Text = ">>";
		this.btnNext.UseVisualStyleBackColor = true;
		this.btnNext.Click += new System.EventHandler(btnNext_Click);
		this.nudLeft.DecimalPlaces = 2;
		this.nudLeft.Location = new System.Drawing.Point(55, 3);
		this.nudLeft.Maximum = new decimal(new int[4] { 260, 0, 0, 0 });
		this.nudLeft.Name = "nudLeft";
		this.nudLeft.Size = new System.Drawing.Size(55, 21);
		this.nudLeft.TabIndex = 2;
		this.nudLeft.ValueChanged += new System.EventHandler(Range_ValueChanged);
		this.nudRight.DecimalPlaces = 2;
		this.nudRight.Location = new System.Drawing.Point(55, 26);
		this.nudRight.Maximum = new decimal(new int[4] { 260, 0, 0, 0 });
		this.nudRight.Name = "nudRight";
		this.nudRight.Size = new System.Drawing.Size(55, 21);
		this.nudRight.TabIndex = 3;
		this.nudRight.ValueChanged += new System.EventHandler(Range_ValueChanged);
		this.pictureBox1.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right;
		this.pictureBox1.Location = new System.Drawing.Point(116, 3);
		this.pictureBox1.MinimumSize = new System.Drawing.Size(265, 0);
		this.pictureBox1.Name = "pictureBox1";
		this.pictureBox1.Size = new System.Drawing.Size(767, 41);
		this.pictureBox1.TabIndex = 4;
		this.pictureBox1.TabStop = false;
		this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(pictureBox1_Paint);
		this.pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(pictureBox1_MouseDown);
		this.pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(pictureBox1_MouseMove);
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 12f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.Controls.Add(this.pictureBox1);
		base.Controls.Add(this.nudRight);
		base.Controls.Add(this.nudLeft);
		base.Controls.Add(this.btnNext);
		base.Controls.Add(this.btnLast);
		this.MinimumSize = new System.Drawing.Size(300, 30);
		base.Name = "MassDisallowedControl";
		base.Size = new System.Drawing.Size(883, 49);
		((System.ComponentModel.ISupportInitialize)this.nudLeft).EndInit();
		((System.ComponentModel.ISupportInitialize)this.nudRight).EndInit();
		((System.ComponentModel.ISupportInitialize)this.pictureBox1).EndInit();
		base.ResumeLayout(false);
	}
}
